<?php

namespace App\Http\Controllers\Apps;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class Home extends Controller
{
    public function login(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'password'  => 'required'
        ]);

        if ($validator->fails()){

            return self::outJsonMsg(1,'验证错误');
        }

        if ($post['password'] != env('PASSWORD',null)){

            return self::outJsonMsg(1,'密码错误');
        }

        Session::put('user',true);

        return self::outJsonMsg(0,'登陆成功');
    }

    public function index()
    {
        if (! Session::get('user',null)){

            return redirect()->route('login');
        }

        return view('home');
    }

    public function list(Request $request)
    {
        if (! Session::get('user',null)){

            return redirect()->route('login');
        }

        return response(Http::get('https://bpm.weishao.com.cn/api/public/v1/process/group/process/list',[
            'secret_key'    => 'ab3d8cb8ecb95e78',
            'sch_code'      => 'jdjsxy'
        ])->body())->header('Content-Type','application/json');
    }

    public function record(Request $request)
    {
        if (! Session::get('user',null)){

            return redirect()->route('login');
        }

        return response(Http::get('https://bpm.weishao.com.cn/api/public/v1/process/instance/77',[
            'secret_key'    => 'ab3d8cb8ecb95e78',
            'sch_code'      => 'jdjsxy',
            'page'          => $request->input('page',1),
            'paginate_by'   => $request->input('num',20),
        ])->body())->header('Content-Type','application/json');
    }

    public function detail(Request $request)
    {
        if (! Session::get('user',null)){

            return redirect()->route('login');
        }
        
        return response(Http::get('https://bpm.weishao.com.cn/api/public/v1/process/task/user/detail/'.$request->input('id'),[
            'secret_key'    => 'ab3d8cb8ecb95e78',
            'sch_code'      => 'jdjsxy'
        ])->body())->header('Content-Type','application/json');
    }

}
